﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VanTaiBLL;
using VanTaiData;
using VanTaiProperty;
using Roles;
using System.Web.UI.HtmlControls;

namespace VanTaiTMS.Page
{
    public partial class khach_hang : System.Web.UI.Page
    {
        #region[Define]
        private DataTable dtCustomer;
        private DataTable dtCustomerDetailEdit;
        private DataTable dtProvince;
        private DataTable dtDepartment;
        private DataTable dtTypesOfBusinesses;
        private DataTable dtRepairMaterial;
        private DataTable dtSearch;
        protected string SuggestionList;
        protected string StrAlert = "";
        private DataTable dtAutoComplete;
        private static bool flag = false;
        private clsTMProvinceBusiness clsProvince = new clsTMProvinceBusiness();
        private clsTMDistrictBusiness clsDistric = new clsTMDistrictBusiness();
        private clsTMDepartmentBusiness clsDepartment = new clsTMDepartmentBusiness();
        private clsTMTypesOfBusinessesBusiness clsTypesOfBusinesses = new clsTMTypesOfBusinessesBusiness();
        private clsTMSymbolRepairMaterialsBusiness clsRepairMaterial = new clsTMSymbolRepairMaterialsBusiness();
        private clsTMCustomersBusiness clsCustommer = new clsTMCustomersBusiness();
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPermisstion();
            Autocomplate();
           
            if (!IsPostBack)
            {
                BindGridCustomer();
                BindGridProvince();
                BindGridDeparment();
                BindGridTypesOfBusinesses();
                BindGridRepairMaterial();
                PanelChonSoDienThoai.Visible = false;
                txtSoDienThoai10.Visible = false;
                txtSoDienThoai11.Visible = false;
            }
        }
        protected void cmdAddNew_Click(object sender, EventArgs e)
        {
            flag = true;
            lbThongBaoLoi.Text = "";
            txtMaKhachHang.Enabled = true;
            ClearTextBox();
            OpenDialog("#danhsachkhachhang");
            imgSave.ToolTip = "Thêm mới";
            upNewUpdatePanel.Update();
            imgSave.Visible = true;
        }
        protected void cmdEdit_Click(object sender, EventArgs e)
        {
            lbThongBaoLoi.Text = "";
            txtMaKhachHang.Enabled = false;
            for (int i = 0; i < ListViewAll.Items.Count; i++)
            {
                CheckBox chkView = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
                Label lbTrangThai = (Label)ListViewAll.Items[i].FindControl("lbDuyet");
                if (chkView != null)
                {
                    if (chkView.Checked)
                    {
                        BindGridCustomerDetail_Edit(Convert.ToInt32(ListViewAll.DataKeys[i].Value.ToString()));
                        OpenDialog("#danhsachkhachhang");
                        upNewUpdatePanel.Update();
                        imgSave.ToolTip = "Cập nhật";
                        flag = false;
                    }
                }
            }
        }
        protected void cmdDel_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < ListViewAll.Items.Count; i++)
            {
                CheckBox chkHide = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
                Label lbTrangThai = (Label)ListViewAll.Items[i].FindControl("lbDuyet");
                if (chkHide != null)
                {
                    if (chkHide.Checked)
                    {
                        try
                        {
                            if (clsCustommer.TMCustomers_Check_Delete_TMCommandTransferOrder(int.Parse(ListViewAll.DataKeys[i].Value.ToString())) == false
                                && clsCustommer.TMCustomers_Check_Delete_TMPlan_TransferOrder(int.Parse(ListViewAll.DataKeys[i].Value.ToString())) == false
                                && clsCustommer.TMCustomers_Check_Delete_TMSalesReport(int.Parse(ListViewAll.DataKeys[i].Value.ToString())) == false
                                && clsCustommer.TMCustomers_Check_Contract(int.Parse(ListViewAll.DataKeys[i].Value.ToString())) == false)
                                
                            {
                                VanTaiBLL.clsTMCustomersBusiness.DeleteTMCustomers(int.Parse(ListViewAll.DataKeys[i].Value.ToString()));
                                BindGridCustomer();
                            }
                            else
                            {
                                lbThongBaoXoa.Text = "Mẫu tin này vẩn còn tồn tại.Không thể xoá !";
                                OpenDialog("#thongbaoxoa");
                                upDatePanelThongBaoXoa.Update();
                                cmdAgree.Visible = false;
                                cmdClose.Text = "Đóng lại";
                            }
                        }
                        catch (Exception ex)
                        {
                            lbThongBaoXoa.Text = ex.ToString();
                            OpenDialog("#thongbaoxoa");
                            upDatePanelThongBaoXoa.Update();
                            cmdAgree.Visible = false;
                            cmdClose.Text = "Đóng lại";
                        }
                    }
                }
            }
        }
        protected void cmdSerach_Click(object sender, EventArgs e)
        {
            if (txtSearch.Text == "")
            {
                StrAlert += "<div class='alert alert-warning'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Vui lòng nhập vào từ khoá để tìm kiếm !";
                StrAlert += "</div>";
            }
            else
            {
                Search(txtSearch.Text);
            }
        }
        protected void cmdRefesh_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            BindGridCustomer();
        }
        protected void cmdExport_Click(object sender, EventArgs e)
        {
            OpenDialog("#exporttoexcel");
        }
        protected void cmdExportToExcel_Click(object sender, EventArgs e)
        {
        }
        //---------------button-popup---------------------
        protected void imgrefesh_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            if (txtMaKhachHang.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào mã khách hàng !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtTenKhachHang.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào tên khách hàng !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtNgaySinh.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào ngày sinh khách hàng !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            
            if (drChonSoDienThoai.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn số điện thoại !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            
            if (txtSoDienThoai10.Text == "" || txtSoDienThoai11.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào số điện thoại khách hàng !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtNGayKy.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào ngày ký !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtNgayThanhLy.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào ngày thanh lý !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (drTinhThanh.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn tĩnh / thành !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (drQuanHuyen.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn Quận/Huyện !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (drBoPhan.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn bộ phận !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (drLoaiDaiLy.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn loại đại lý/DN !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (drLinhVuc.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn lĩnh vực !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            
            if (txtMaKhachHang.Text != "" && txtTenKhachHang.Text != ""
                && txtNgaySinh.Text != "" && txtNGayKy.Text != ""
                && txtNgayThanhLy.Text != ""
                && drTinhThanh.SelectedValue.ToString() != "-1"
                && drQuanHuyen.SelectedValue.ToString() != "-1"
                && drBoPhan.SelectedValue.ToString() != "-1"
                && drLoaiDaiLy.SelectedValue.ToString() != "-1"
                && drLinhVuc.SelectedValue.ToString() != "-1"
                && drChonSoDienThoai.SelectedValue.ToString() != "-1"
                && txtSoDienThoai10.Text != "" || txtSoDienThoai11.Text != ""
                && txtDiaChi.Text != "")
            {
                Insert();
            }
        }
        protected void cmdAgree_Click(object sender, EventArgs e)
        {
            
        }
        protected void cmdClose_Click(object sender, EventArgs e)
        {
            CloseDialog("#thongbaoxoa");
        }
        protected void cmdCloseThongBao_Click(object sender, EventArgs e)
        {
            CloseDialog("#thongbao");
        }
        protected void ImgRefeshDuyet_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void ImgRefeshExport_Click(object sender, ImageClickEventArgs e)
        {
            //txtTuNgayNhapKhoExport.Text = "";
            //txtDenNgayNhapKhoExport.Text = "";
            //BindGridLenhDieuDongExportExcel();
        }
        //-------------list-view-----------------------
        protected void ListViewAll_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            try
            {
                HttpCookie cookie = Request.Cookies["UserLogin"];
                bool Edit = SecurityServices.HasPermisstion(StringContent.QuanLyDanhKhachHang, int.Parse(cookie["RolesID"]), TypeaAudit.Edit);
                lbThongBaoLoi.Text = "";
                txtMaKhachHang.Enabled = false;
                if (e.CommandName == "Accept")
                {
                    if (Edit == false)
                    {
                        imgSave.Visible = false;
                        string SenderId = ((HtmlInputHidden)e.Item.FindControl("hiddenId")).Value;
                        BindGridCustomerDetail_Edit(int.Parse(SenderId));
                        OpenDialog("#danhsachkhachhang");
                        upNewUpdatePanel.Update();
                        imgSave.ToolTip = "Cập nhật";
                        flag = false;
                    }
                    else
                    {
                        imgSave.Visible = true;
                        string SenderId = ((HtmlInputHidden)e.Item.FindControl("hiddenId")).Value;
                        BindGridCustomerDetail_Edit(int.Parse(SenderId));
                        OpenDialog("#danhsachkhachhang");
                        upNewUpdatePanel.Update();
                        imgSave.ToolTip = "Cập nhật";
                        flag = false;
                    }
                }
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        protected void ListViewAll_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                LinkButton linkListName = (LinkButton)e.Item.FindControl("linkListName");
                string _jsDouble1 = ClientScript.GetPostBackClientHyperlink(linkListName, "");


                linkListName.Attributes["ondblclick"] = _jsDouble1;
                linkListName.Attributes["onclick"] = "return false;";
            }
        }
        //-----------pagger-----------------------
        protected void DataPagerListAll_PreRender(object sender, EventArgs e)
        {
            DataPagerListAll.PageSize = 17;
            BindGridCustomer();
        }
        //---------Check-all--------------
        protected void chkCheckAll_CheckedChanged(object sender, EventArgs e)
        {

        }
        //---------Dropdownlist-----------
        protected void drChonSoDienThoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drChonSoDienThoai.SelectedValue.ToString() == "1")
            {
                PanelChonSoDienThoai.Visible = true;
                txtSoDienThoai10.Visible = true;
                txtSoDienThoai11.Visible = false;
            }
            if (drChonSoDienThoai.SelectedValue.ToString() == "2")
            {
                PanelChonSoDienThoai.Visible = true;
                txtSoDienThoai10.Visible = false;
                txtSoDienThoai11.Visible = true;
            }
            if (drChonSoDienThoai.SelectedValue.ToString() == "-1")
            {
                PanelChonSoDienThoai.Visible = false;
                txtSoDienThoai10.Visible = false;
                txtSoDienThoai11.Visible = false;
                txtSoDienThoai10.Text = "";
                txtSoDienThoai11.Text = "";
            }
        }
        protected void drTinhThanh_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridDrQuanHuyen();
        }
        //------------Thêm-Nhanh-----------------
        protected void linkThemNhanhTinhThanh_Click(object sender, EventArgs e)
        {
            OpenDialog("#themnhanhtinhthanh");
        }
        protected void linkThemNhanhQuanHuyen_Click(object sender, EventArgs e)
        {
            OpenDialog("#themnhanhquanhuyen");
        }
        protected void linkThemNhanhBoPhan_Click(object sender, EventArgs e)
        {
            OpenDialog("#themnhanhbophan");
        }
        protected void linkThemNhanhLoaiDaiLy_Click(object sender, EventArgs e)
        {
            OpenDialog("#themnhanhloaidailydoanhnghiep");
        }
        protected void linkThemNhanhLinhVuc_Click(object sender, EventArgs e)
        {
            OpenDialog("#themnhanhlinhvuc");
        }
        #endregion

        #region[Function]
        protected void BindGridCustomer()
        {
            dtCustomer = clsCustommer.Search(txtSearch.Text);
            if (dtCustomer != null && dtCustomer.Rows.Count > 0)
            {
                ListViewAll.DataSource = dtCustomer;
                ListViewAll.DataBind();
                ListViewAll.Visible = true;
                DataPagerListAll.Visible = true;
            }
            else
            {
                ListViewAll.Visible = false;
                DataPagerListAll.Visible = false;
            }
        }
        protected void BindGridCustomerDetail_Edit(int id)
        {
            dtCustomerDetailEdit = clsCustommer.BindGirdTMCustomers_Detail_Edit(id);
            if (dtCustomerDetailEdit != null && dtCustomerDetailEdit.Rows.Count > 0)
            {
                lbID.Text = dtCustomerDetailEdit.Rows[0]["Customers_ID"].ToString();
                lbIDXoa.Text = dtCustomerDetailEdit.Rows[0]["Customers_ID"].ToString();
                txtMaKhachHang.Text = dtCustomerDetailEdit.Rows[0]["Customers_Alias"].ToString();
                txtTenKhachHang.Text = dtCustomerDetailEdit.Rows[0]["Customers_Name"].ToString();
                #region
                if (dtCustomerDetailEdit.Rows[0]["Customers_Brithday"] == DBNull.Value)
                {
                    txtNgaySinh.Text = "";
                }
                else
                {
                    txtNgaySinh.Text = dtCustomerDetailEdit.Rows[0]["Customers_Brithday"].ToString();
                }

                if (dtCustomerDetailEdit.Rows[0]["Customers_Phone_Number"] == DBNull.Value)
                {
                    drChonSoDienThoai.SelectedIndex = -1;
                    txtSoDienThoai11.Text = "";
                    txtSoDienThoai10.Text = "";
                }
                if (dtCustomerDetailEdit.Rows[0]["Customers_Phone_Number"].ToString().Length == 14)
                {
                    PanelChonSoDienThoai.Visible = true;
                    txtSoDienThoai10.Visible = true;
                    txtSoDienThoai11.Visible = false;
                    txtSoDienThoai10.Text = dtCustomerDetailEdit.Rows[0]["Customers_Phone_Number"].ToString();
                    drChonSoDienThoai.SelectedValue = "1";
                }

                if (dtCustomerDetailEdit.Rows[0]["Customers_Phone_Number"].ToString().Length == 15)
                {
                    PanelChonSoDienThoai.Visible = true;
                    txtSoDienThoai10.Visible = false;
                    txtSoDienThoai11.Visible = true;
                    txtSoDienThoai11.Text = dtCustomerDetailEdit.Rows[0]["Customers_Phone_Number"].ToString();
                    drChonSoDienThoai.SelectedValue = "2";
                }
                if (dtCustomerDetailEdit.Rows[0]["Customers_Tax_Code"] == DBNull.Value)
                {
                    txtMaSoThue.Text = "";
                }
                else
                {
                    txtMaSoThue.Text = dtCustomerDetailEdit.Rows[0]["Customers_Tax_Code"].ToString();
                }
                if (dtCustomerDetailEdit.Rows[0]["Customers_Address"] == DBNull.Value)
                {
                    txtDiaChi.Text = "";
                }
                else
                {
                    txtDiaChi.Text = dtCustomerDetailEdit.Rows[0]["Customers_Address"].ToString();
                }
                #endregion
                txtMaDaiLy.Text = dtCustomerDetailEdit.Rows[0]["Customers_Dealer_ID"].ToString();
                txtSoHopDong.Text = dtCustomerDetailEdit.Rows[0]["Customers_Minutes_Number"].ToString();
                txtTenGiamDocTruongPhong.Text = dtCustomerDetailEdit.Rows[0]["Customers_General_Name"].ToString();
                txtNGayKy.Text = dtCustomerDetailEdit.Rows[0]["Customers_Day_Signed"].ToString();
                txtNgayThanhLy.Text = dtCustomerDetailEdit.Rows[0]["Customers_Day_Liquidation"].ToString();
                #region
                if (dtCustomerDetailEdit.Rows[0]["Customers_Province_ID"] != DBNull.Value)
                {
                    drTinhThanh.SelectedValue = dtCustomerDetailEdit.Rows[0]["Customers_Province_ID"].ToString();
                }
                else
                {
                    drTinhThanh.SelectedIndex = -1;
                }
                if (dtCustomerDetailEdit.Rows[0]["Customers_Districts_ID"] != DBNull.Value)
                {
                    drQuanHuyen.Items.Clear();
                    drQuanHuyen.Items.Add(new ListItem("--Vui lòng chọn quận huyện--", "-1"));
                    drQuanHuyen.DataSource = clsDistric.BindGirdTMDistrictDetail((int)dtCustomerDetailEdit.Rows[0]["Customers_Districts_ID"]);
                    drQuanHuyen.DataTextField = "TenQuanHuyen";
                    drQuanHuyen.DataValueField = "STT";
                    drQuanHuyen.DataBind();

                    drQuanHuyen.SelectedIndex = 1;
                }
                else
                {
                    drQuanHuyen.SelectedIndex = -1;
                }
                if (dtCustomerDetailEdit.Rows[0]["Customers_Division_ID"] != DBNull.Value)
                {
                    drBoPhan.SelectedValue = dtCustomerDetailEdit.Rows[0]["Customers_Division_ID"].ToString();
                }
                else
                {
                    drBoPhan.SelectedIndex = -1;
                }
                if (dtCustomerDetailEdit.Rows[0]["Customers_Type_DN_ID"] != DBNull.Value)
                {
                    drLoaiDaiLy.SelectedValue = dtCustomerDetailEdit.Rows[0]["Customers_Type_DN_ID"].ToString();
                }
                else
                {
                    drLoaiDaiLy.SelectedIndex = -1;
                }
                if (dtCustomerDetailEdit.Rows[0]["Customers_Alternative_Materials_ID"] != DBNull.Value)
                {
                    drLinhVuc.SelectedValue = dtCustomerDetailEdit.Rows[0]["Customers_Alternative_Materials_ID"].ToString();
                }
                else
                {
                    drLinhVuc.SelectedIndex = -1;
                }

                #endregion
                txtSoFax.Text = dtCustomerDetailEdit.Rows[0]["Customers_Fax"].ToString();
                txtEmail.Text = dtCustomerDetailEdit.Rows[0]["Customers_Email"].ToString();
                txtSoTaiKhoan.Text = dtCustomerDetailEdit.Rows[0]["Customers_Account_Code"].ToString();
                txtNganHang.Text = dtCustomerDetailEdit.Rows[0]["Customers_Person_Responsible"].ToString();
                txtGhiChu.Text = dtCustomerDetailEdit.Rows[0]["Customers_Note"].ToString();
            }
        }
        protected void BindGridProvince()
        {
            dtProvince = clsProvince.BindGirdTMProvince(txtSearch.Text);
            if (dtProvince != null && dtProvince.Rows.Count > 0)
            {
                drTinhThanh.DataSource = dtProvince;
                drTinhThanh.DataTextField = "TenTinh";
                drTinhThanh.DataValueField = "STT";
                drTinhThanh.DataBind();
            }
        }
        protected void BindGridDrQuanHuyen()
        {
            drQuanHuyen.Items.Clear();
            drQuanHuyen.Items.Add(new ListItem("--Vui lòng chọn quận huyện--", "-1"));
            drQuanHuyen.DataSource = clsDistric.BindGridTMDistrictDropdownlist(int.Parse(drTinhThanh.SelectedValue.ToString()));
            drQuanHuyen.DataTextField = "TenQuanHuyen";
            drQuanHuyen.DataValueField = "STT";
            drQuanHuyen.DataBind();

        }
        protected void BindGridDeparment()
        {
            dtDepartment = clsDepartment.BindGirdTMDepartment();
            if (dtDepartment != null && dtDepartment.Rows.Count > 0)
            {
                drBoPhan.DataSource = dtDepartment;
                drBoPhan.DataTextField = "Department_Name";
                drBoPhan.DataValueField = "Department_ID";
                drBoPhan.DataBind();
            }
        }
        protected void BindGridTypesOfBusinesses()
        {
            dtTypesOfBusinesses = clsTypesOfBusinesses.BindGirdTMTypesOfBusinesses();
            if (dtTypesOfBusinesses != null && dtTypesOfBusinesses.Rows.Count > 0)
            {
                drLoaiDaiLy.DataSource = dtTypesOfBusinesses;
                drLoaiDaiLy.DataTextField = "TypesOfBusinesses_Name";
                drLoaiDaiLy.DataValueField = "TypesOfBusinesses_ID";
                drLoaiDaiLy.DataBind();
            }
        }
        protected void BindGridRepairMaterial()
        {
            dtRepairMaterial = clsRepairMaterial.BindGirdTMSymbolRepairMaterials();
            if (dtRepairMaterial != null && dtRepairMaterial.Rows.Count > 0)
            {
                drLinhVuc.DataSource = dtRepairMaterial;
                drLinhVuc.DataTextField = "SymbolRepairMaterials_Name";
                drLinhVuc.DataValueField = "SymbolRepairMaterials_ID";
                drLinhVuc.DataBind();
            }
        }
        protected void Insert()
        {
            //Insert
            if (flag)
            {
                InsertCustomer();
            }
            else//Update
            {
                UpdateCustomer();
            }
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            dtCustomer = clsCustommer.BindGirdTMCustomers();
            if (dtCustomer != null && dtCustomer.Rows.Count > 0)
            {
                foreach (DataRow dr in dtCustomer.Rows)
                {
                    if (txtMaKhachHang.Text == dr["Customers_Alias"].ToString())
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertCustomer()
        {
            try
            {
                if (CheckInsert())
                {
                    lbThongBaoLoi.Text = "Mã khách hàng này đã tồn tại !";
                    OpenDialog("#thongbao");
                    updatePanelThongBao.Update();
                }
                else
                {
                    VanTaiData.clsTMCustomersProperty obj = new clsTMCustomersProperty();
                    obj.Customers_Alias = txtMaKhachHang.Text;
                    obj.Customers_Name = txtTenKhachHang.Text;
                    obj.Customers_Brithday = DateTime.Parse(txtNgaySinh.Text);
                    obj.Customers_Tax_Code = txtMaSoThue.Text;
                    obj.Customers_Dealer_ID = txtMaDaiLy.Text;
                    obj.Customers_Minutes_Number = txtSoHopDong.Text;
                    obj.Customers_General_Name = txtTenGiamDocTruongPhong.Text;
                    obj.Customers_Day_Signed = DateTime.Parse(txtNGayKy.Text);
                    obj.Customers_Day_Liquidation = DateTime.Parse(txtNgayThanhLy.Text);
                    obj.Customers_Province_ID = int.Parse(drTinhThanh.SelectedValue.ToString());
                    obj.Customers_Districts_ID = int.Parse(drQuanHuyen.SelectedValue.ToString());
                    obj.Customers_Division_ID = int.Parse(drBoPhan.SelectedValue.ToString());
                    obj.Customers_Type_DN_ID = int.Parse(drLoaiDaiLy.SelectedValue.ToString());
                    obj.Customers_Alternative_Materials_ID = int.Parse(drLinhVuc.SelectedValue.ToString());
                    obj.Customers_Person_Responsible = txtNganHang.Text;
                    obj.Customers_Address = txtDiaChi.Text;
                    obj.Customers_Note = txtGhiChu.Text;
                    #region
                    if (drChonSoDienThoai.SelectedValue == "1")
                    {
                        PanelChonSoDienThoai.Visible = true;
                        txtSoDienThoai10.Visible = true;
                        txtSoDienThoai11.Visible = false;
                        obj.Customers_Phone_Number = txtSoDienThoai10.Text;
                    }
                    if (drChonSoDienThoai.SelectedValue == "2")
                    {
                        PanelChonSoDienThoai.Visible = true;
                        txtSoDienThoai10.Visible = false;
                        txtSoDienThoai11.Visible = true;
                        obj.Customers_Phone_Number = txtSoDienThoai11.Text;
                    }
                    #endregion
                    obj.Customers_Fax = txtSoFax.Text;
                    obj.Customers_Email = txtEmail.Text;
                    obj.Customers_Account_Code = txtSoTaiKhoan.Text;
                    //------------------
                    obj.Customers_Name_C = DBNull.Value.ToString();
                    obj.Customers_Name_E = DBNull.Value.ToString();
                    obj.Customers_Trade_Names = DBNull.Value.ToString();
                    obj.Customers_CMT = DBNull.Value.ToString();
                    obj.Customers_Dational_ID = 0;
                    obj.Customers_Domain_ID = 0;
                    obj.Customers_Status = 0;
                    obj.Customers_Degree = 0;
                    obj.Customers_Subjective_Units = DBNull.Value.ToString();
                    obj.Customers_Supply_ID = 0;
                    obj.Customers_Person_Responsible = DBNull.Value.ToString();
                    obj.Customers_Status_Review = false;
                    obj.Customers_Branch_ID = 2;
                    VanTaiBLL.clsTMCustomersBusiness.InsertTMCustomers(obj);
                    lbThongBaoLoi.Text = "Thêm mẫu tin thành công !";
                    OpenDialog("#thongbao");
                    updatePanelThongBao.Update();
                    BindGridCustomer();
                    ClearTextBox();
                }
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        protected void UpdateCustomer()
        {
            try
            {
                VanTaiData.clsTMCustomersProperty obj = new clsTMCustomersProperty();
                obj.Customers_ID = int.Parse(lbID.Text);
                obj.Customers_Alias = txtMaKhachHang.Text;
                obj.Customers_Name = txtTenKhachHang.Text;
                obj.Customers_Brithday = DateTime.Parse(txtNgaySinh.Text);
                obj.Customers_Tax_Code = txtMaSoThue.Text;
                obj.Customers_Dealer_ID = txtMaDaiLy.Text;
                obj.Customers_Minutes_Number = txtSoHopDong.Text;
                obj.Customers_General_Name = txtTenGiamDocTruongPhong.Text;
                obj.Customers_Day_Signed = DateTime.Parse(txtNGayKy.Text);
                obj.Customers_Day_Liquidation = DateTime.Parse(txtNgayThanhLy.Text);
                obj.Customers_Province_ID = int.Parse(drTinhThanh.SelectedValue.ToString());
                obj.Customers_Districts_ID = int.Parse(drQuanHuyen.SelectedValue.ToString());
                obj.Customers_Division_ID = int.Parse(drBoPhan.SelectedValue.ToString());
                obj.Customers_Type_DN_ID = int.Parse(drLoaiDaiLy.SelectedValue.ToString());
                obj.Customers_Alternative_Materials_ID = int.Parse(drLinhVuc.SelectedValue.ToString());
                obj.Customers_Person_Responsible = txtNganHang.Text;
                obj.Customers_Address = txtDiaChi.Text;
                obj.Customers_Note = txtGhiChu.Text;
                #region
                if (drChonSoDienThoai.SelectedValue == "1")
                {
                    PanelChonSoDienThoai.Visible = true;
                    txtSoDienThoai10.Visible = true;
                    txtSoDienThoai11.Visible = false;
                    obj.Customers_Phone_Number = txtSoDienThoai10.Text;
                }
                if (drChonSoDienThoai.SelectedValue == "2")
                {
                    PanelChonSoDienThoai.Visible = true;
                    txtSoDienThoai10.Visible = false;
                    txtSoDienThoai11.Visible = true;
                    obj.Customers_Phone_Number = txtSoDienThoai11.Text;
                }
                #endregion
                obj.Customers_Fax = txtSoFax.Text;
                obj.Customers_Email = txtEmail.Text;
                obj.Customers_Account_Code = txtSoTaiKhoan.Text;
                //------------------
                obj.Customers_Name_C = DBNull.Value.ToString();
                obj.Customers_Name_E = DBNull.Value.ToString();
                obj.Customers_Trade_Names = DBNull.Value.ToString();
                obj.Customers_CMT = DBNull.Value.ToString();
                obj.Customers_Dational_ID = 0;
                obj.Customers_Domain_ID = 0;
                obj.Customers_Status = 0;
                obj.Customers_Degree = 0;
                obj.Customers_Subjective_Units = DBNull.Value.ToString();
                obj.Customers_Supply_ID = 0;
                obj.Customers_Person_Responsible = DBNull.Value.ToString();
                obj.Customers_Status_Review = false;
                obj.Customers_Branch_ID = 2;
                VanTaiBLL.clsTMCustomersBusiness.UpdateTMCustomers(obj);
                lbThongBaoLoi.Text = "Cập nhật mẫu tin thành công !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
                BindGridCustomer();
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        //AutoComplate
        protected void Autocomplate()
        {
            dtAutoComplete = clsCustommer.BindGirdTMCustomers();
            if (dtAutoComplete != null && dtAutoComplete.Rows.Count > 0)
            {
                foreach (DataRow dr in dtAutoComplete.Rows)
                {
                    if (string.IsNullOrEmpty(SuggestionList))
                    {
                        SuggestionList += "\"" + dr["Customers_Alias"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Customers_Name"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Customers_Address"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Customers_Email"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Customers_Phone_Number"].ToString().Replace("\n", " ") + "\",";
                    }
                    else
                    {
                        SuggestionList += "\"" + dr["Customers_Alias"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Customers_Name"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Customers_Address"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Customers_Email"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Customers_Phone_Number"].ToString().Replace("\n", " ") + "\",";
                    }
                }
            }
        }
        protected void Search(string keywords)
        {
            dtSearch = clsCustommer.Search(keywords);
            if (dtSearch != null && dtSearch.Rows.Count > 0)
            {
                ListViewAll.DataSource = dtSearch;
                ListViewAll.DataBind();
                ListViewAll.Visible = true;
                DataPagerListAll.Visible = true;

                StrAlert += "<div class='alert alert-success'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Tìm thấy " + dtSearch.Rows.Count.ToString() + " mẫu tin !";
                StrAlert += "</div>";
            }
            else
            {
                ListViewAll.Visible = false;
                DataPagerListAll.Visible = false;
                StrAlert += "<div class='alert alert-error'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Không tìm thấy mẫu tin nào !";
                StrAlert += "</div>";
            }
        }
        //Total
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        #endregion

        #region[Clear-TextBox]
        protected void ClearTextBox()
        {
            txtMaKhachHang.Text = "";
            txtTenKhachHang.Text = "";
            txtNgaySinh.Text = "";
            txtMaSoThue.Text = "";
            txtMaDaiLy.Text = "";
            txtSoHopDong.Text = "";
            txtTenGiamDocTruongPhong.Text = "";
            txtNGayKy.Text = "";
            txtNgayThanhLy.Text = "";
            drTinhThanh.SelectedIndex = -1;
            drQuanHuyen.SelectedIndex = -1;
            drBoPhan.SelectedIndex = -1;
            drLoaiDaiLy.SelectedIndex = -1;
            drLinhVuc.SelectedIndex = -1;
            txtSoDienThoai10.Text = "";
            txtSoDienThoai11.Text = "";
            txtSoFax.Text = "";
            txtEmail.Text = "";
            txtSoTaiKhoan.Text = "";
            txtNganHang.Text = "";
            txtDiaChi.Text = "";
            txtGhiChu.Text = "";
        }
        #endregion

        #region[Permisstion]
        protected void CheckPermisstion()
        {
            HttpCookie cookie = Request.Cookies["UserLogin"];
            if (cookie != null)
            {
                bool Is_Page_All = SecurityServices.HasPermisstionPage(StringContent.QuanLyDanhKhachHang, int.Parse(cookie["RolesID"]), true);
                if (Is_Page_All == true)
                {
                    bool AddNew = SecurityServices.HasPermisstion(StringContent.QuanLyDanhKhachHang, int.Parse(cookie["RolesID"]), TypeaAudit.AddNew);
                    if (AddNew == false)
                    {
                        cmdAddNew.Visible = false;
                    }
                    else
                    {
                        cmdAddNew.Visible = true;
                    }
                    bool Edit = SecurityServices.HasPermisstion(StringContent.QuanLyDanhKhachHang, int.Parse(cookie["RolesID"]), TypeaAudit.Edit);
                    if (Edit == false)
                    {
                        cmdEdit.Visible = false;
                    }
                    else
                    {
                        cmdEdit.Visible = true;
                    }
                    bool Del = SecurityServices.HasPermisstion(StringContent.QuanLyDanhKhachHang, int.Parse(cookie["RolesID"]), TypeaAudit.Delete);
                    if (Del == false)
                    {
                        cmdDel.Visible = false;
                    }
                    else
                    {
                        cmdDel.Visible = true;
                    }
                }
                else
                {
                    Response.Redirect("error-page.aspx");
                }
            }
        }
        #endregion
    }
}